//
//  NSAttributedString+AG.swift
//  AG
//
//  Created by zhuxuhong on 2020/10/22.
//  Copyright © 2020 AgoCulture. All rights reserved.
//

import UIKit

extension NSAttributedString {
    public typealias ParagraphStyle = (NSMutableParagraphStyle)->Void
    public static func bw_with(
        text: String,
        font: UIFont? = .system14,
        color: UIColor? = .black,
        defaultParagraph: Bool = false,
        paragraph: ParagraphStyle? = nil,
        attrs: [Key: Any]? = nil
    ) -> NSAttributedString {
        var attributes: [Key: Any] = [:]
        if defaultParagraph {
            let pg = NSMutableParagraphStyle()
            pg.lineHeightMultiple = 1.2
            attributes[Key.paragraphStyle] = pg
        }
        if let handler = paragraph {
            let pg = NSMutableParagraphStyle()
            handler(pg)
            attributes[Key.paragraphStyle] = pg
        }
        if let font = font {
            attributes[Key.font] = font
        }
        if let color = color {
            attributes[Key.foregroundColor] = color
        }
        if let attrs = attrs {
            attrs.forEach {
                attributes[$0] = $1
            }
        }
        return .init(string: text, attributes: attributes)
    }
    
    public static func bw_by(_ comps: [NSAttributedString]) -> NSAttributedString{
        let mattr = NSMutableAttributedString()
        comps.forEach {
            mattr.append($0)
        }
        return mattr
    }
}
